﻿'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
'
'Copyright (c) 2012, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Will Strohl, Simple Redirect, nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Exceptions

Imports WillStrohl.Modules.SimpleRedirect.RedirectInfoMembers

Namespace WillStrohl.Modules.SimpleRedirect

    <Serializable()> _
    Public NotInheritable Class RedirectInfo
        Implements IRedirectInfo, IHydratable

#Region " Private Members "

        Private p_RedirectId As Integer = Null.NullInteger
        Private p_ModuleId As Integer = Null.NullInteger
        Private p_RedirectFrom As String = Null.NullString
        Private p_RedirectTo As String = Null.NullString
        Private p_Visits As Integer = Null.NullInteger

#End Region

#Region " Public Properties "

        Public Property RedirectId() As Integer Implements IRedirectInfo.RedirectId
            Get
                Return p_RedirectId
            End Get
            Set(ByVal value As Integer)
                p_RedirectId = value
            End Set
        End Property

        Public Property ModuleId() As Integer Implements IRedirectInfo.ModuleId
            Get
                Return p_ModuleId
            End Get
            Set(ByVal value As Integer)
                p_ModuleId = value
            End Set
        End Property

        Public Property RedirectFrom As String Implements IRedirectInfo.RedirectFrom
            Get
                Return p_RedirectFrom
            End Get
            Set(ByVal value As String)
                p_RedirectFrom = value
            End Set
        End Property

        Public Property RedirectTo As String Implements IRedirectInfo.RedirectTo
            Get
                Return p_RedirectTo
            End Get
            Set(ByVal value As String)
                p_RedirectTo = value
            End Set
        End Property

        Public Property Visits As Integer Implements IRedirectInfo.Visits
            Get
                Return p_Visits
            End Get
            Set(value As Integer)
                p_Visits = value
            End Set
        End Property

#End Region

#Region " Constructors "

        Public Sub New()
            ' do nothing
        End Sub

        Public Sub New(ByVal ModuleId As Integer, ByVal RedirectFrom As String, ByVal RedirectTo As String)
            p_ModuleId = ModuleId
            p_RedirectFrom = RedirectFrom
            p_RedirectTo = RedirectTo
        End Sub

        Public Sub New(ByVal RedirectId As Integer, ByVal ModuleId As Integer, ByVal RedirectFrom As String, ByVal RedirectTo As String)
            p_RedirectId = RedirectId
            p_ModuleId = ModuleId
            p_RedirectFrom = RedirectFrom
            p_RedirectTo = RedirectTo
        End Sub

#End Region

#Region " IHydratable Implementation "

        Public Property KeyID() As Integer Implements IHydratable.KeyID
            Get
                Return p_RedirectId
            End Get
            Set(ByVal value As Integer)
                p_RedirectId = value
            End Set
        End Property

        Public Sub Fill(ByVal dr As System.Data.IDataReader) Implements IHydratable.Fill
            Try
                While dr.Read
                    If Not dr.Item(RedirectIdField) Is Nothing Then
                        p_RedirectId = Integer.Parse(dr.Item(RedirectIdField).ToString, Globalization.NumberStyles.Integer)
                    End If
                    If Not dr.Item(ModuleIdField) Is Nothing Then
                        p_ModuleId = Integer.Parse(dr.Item(ModuleIdField).ToString, Globalization.NumberStyles.Integer)
                    End If
                    If Not dr.Item(RedirectFromField) Is Nothing Then
                        p_RedirectFrom = dr.Item(RedirectFromField).ToString
                    End If
                    If Not dr.Item(RedirectToField) Is Nothing Then
                        p_RedirectTo = dr.Item(RedirectToField).ToString
                    End If
                    If Not dr.Item(VisitsField) Is Nothing Then
                        Try
                            p_Visits = Integer.Parse(dr.Item(VisitsField).ToString, Globalization.NumberStyles.Integer)
                        Catch
                            p_Visits = 0
                        End Try
                    End If
                End While
            Catch ex As Exception
                LogException(ex)
            Finally
                If Not dr.IsClosed Then
                    dr.Close()
                End If
            End Try
        End Sub

#End Region

    End Class

End Namespace